<template>
  <div class="mod-config">
    <el-form :inline="true" ref="dataForm" label-width="90px" :model="dataForm" label-suffix=":" @keyup.enter.native="getDataList()">

      <template  v-for="item in queryList">
        <ng-form-item :record="item" :models="dataForm" :key="item.model"  />
      </template> 
      <el-form-item>
        <el-button @click="search()">查询</el-button>
        <el-button @click="reset()">重置</el-button>
        <el-button v-if="isAuth('form:data:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>

      <!--   <el-button v-if="isAuth('form:data:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
     <!--  <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>    -->
       <template  v-for="item in showList">
        <el-table-column  
        header-align="center"
        :key="item.model" 
        align="center" 
        :label="item.label">
          <template slot-scope="scope">
            <ng-form-item-node :record="item" :models="scope.row.formData" :preview="true"  /> 
          </template>
        </el-table-column> 
      </template> 
      <el-table-column
        prop="createId"
        header-align="center"
        align="center"
         width="120"
        label="创建人">
        <template  slot-scope="scope">
          <user :value="scope.row.createId" />
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
         width="150"
        label="创建时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="170"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="preview(scope.row.id)">查看</el-button>
          <el-button type="text" size="small" v-if="isAuth('form:data:update') && userId == scope.row.createId && !scope.row.solveStatus" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
         
          <el-button type="text" size="small" v-if="isAuth('form:data:delete') && !scope.row.solveStatus" @click="deleteHandle(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalCount"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <solve v-if="solveVisible" ref="solve" @refreshDataList="getDataList"/>
   
  </div>
</template>

<script>
  import AddOrUpdate from './add-or-update'
  import cloneDeep from "lodash/cloneDeep"; 
  import {getByCode} from '@/api/form/template'
  import {getList , remove} from '@/api/form/data'
  export default {
    data () {
      return {
        dataForm: { 
          code: ''
        },
        form: {}, 

        //列表显示list
        showList: [],
        // 搜索list
        queryList: [],

        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalCount: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        solveVisible: false
      }
    },
    computed: {  
      userId: {
        get() {
          return this.$store.state.user.id;
        } 
      },
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.dataForm.code =  this.$route.query.code;
       
      this.getDataList();
      this.initForm()
    },
    methods: {
       refresh(){
        // this.$router.push({name:'item_formdata',query: {key:this.dataForm.key,pageIndex:this.pageIndex,pageSize:this.pageSize}});
      },
      initForm() {
       getByCode(this.dataForm.code).then(({data}) => {
          if (data && data.code === 0) {
            if(data.data.templateData) {
             
              const formTemplate_ = JSON.parse(data.data.templateData)

              this.form = data.data
              this.form.templateData = formTemplate_

              const list_ = cloneDeep(formTemplate_.list)


              // 从list中去掉rule
              list_.forEach(t=> {
                delete t.rules

                // textarea 类型替换为input
                if(t.type == 'textarea') {
                  t.type = 'input'
                }

                if(t.options) {
                  t.options.clearable = true
                }

                t.labelWidth = '70'
              })
 

              this.showList =  list_.filter(t=> formTemplate_.config && formTemplate_.config.showList && formTemplate_.config.showList.includes(t.model))   
              this.queryList =  list_.filter(t=> formTemplate_.config && formTemplate_.config.queryList && formTemplate_.config.queryList.includes(t.model))   
            }
          }
        })
      },
      search(){
         this.pageIndex = 1 ;
         this.getDataList();
         this.refresh();
      }, 
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true

        const form_ = {'page': this.pageIndex,
            'limit': this.pageSize,
            'code': this.dataForm.code,
            'query': this.dataForm
            }
 
        getList(form_).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalCount = data.data.totalCount
            if(data.data.page != this.pageIndex) {
            	this.pageIndex = data.data.page
          	}
          } else {
            this.dataList = []
            this.totalCount = 0
            this.pageIndex = 1
          }
         
        }).finally(()=>{
        	this.dataListLoading = false 
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
        this.refresh()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
        this.refresh()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id , this.form)
        })
      },
      preview(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id , this.form , true)
        })
      },
      reset(){
        this.$refs.dataForm.resetFields()
      },
      // 删除
      deleteHandle (row) {
        var ids = row ? [row.id] : this.dataListSelections.map(item => {
          return item.id
        }) 
        this.$confirm(`确定要对数据进行[${row ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          remove(ids).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1000
              })
              this.getDataList()
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
